package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.Menu;
import com.dk.pojo.TPerson;
import com.dk.service.MuneService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menucontroller")
public class MenuController {
    @Reference
    private MuneService muneService;
    //拼接菜单
    @RequestMapping("getTree")
    @ResponseBody
    public  List<Map<String, Object>> getTree(){
        TPerson t = (TPerson) SecurityUtils.getSubject().getPrincipal();
        List<Map<String, Object>> list = muneService.getTree(t);
        return list;
    }

    //跳转ztree页面
    @RequestMapping("toztree")
    public String toZtree(){
        return "ztree/show";
    }

    //拼接ztree
    @RequestMapping("getzTree")
    @ResponseBody
    public List<Map<String,Object>> getZtree(){
        List<Map<String,Object>> list = muneService.getZtree();
        return list;
    }

    //跳转增加菜单页面
    @RequestMapping("toinsert")
    public String toInsert(){
        return "ztree/insert";
    }

    //菜单增加
    @RequestMapping("insertinto")
    @ResponseBody
    public Boolean insertInto(Menu menu,Integer pid){
        menu.setParentid(pid);
        Boolean b = muneService.insertTPerson(menu);
        return b;
    }

    //跳转修改菜单页面
    @RequestMapping("toupdate")
    public String toupDate(Integer pid, Model model){
        //查询当前菜单对象
        Menu menu = muneService.selectById(pid);
        model.addAttribute("menu",menu);
        return "ztree/update";
    }

    //菜单修改
    @RequestMapping("updateinto")
    @ResponseBody
    public Boolean updateInto(Menu menu){
        Boolean b = muneService.updatemEenu(menu);
        return b;
    }

    //菜单删除
    @RequestMapping("deletetree")
    @ResponseBody
    public Boolean deleteTree(Integer id){
        Boolean b = muneService.deleteTree(id);
        return b;
    }

    //拼接角色回显ztree
    @RequestMapping("getzTreeChecked")
    @ResponseBody
    public List<Map<String,Object>> getzTreeChecked(Integer rid){
        List<Map<String,Object>> list = muneService.getzTreeChecked(rid);
        return list;
    }
}
